package mn.idax.exchange.otc.util;

import com.alibaba.fastjson.JSON;
import com.fasterxml.jackson.core.type.TypeReference;
import mn.idax.exchange.otc.constant.CommonConstant;
import mn.idax.exchange.otc.domain.PairMarket;
import mn.idax.exchange.otc.entity.Pair;
import mn.idax.exchange.otc.vo.CoinmarketcapVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;
import java.text.MessageFormat;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.TimeUnit;

import static java.util.Optional.ofNullable;

/**
 * @Auther: dison
 * @Date: 2018/7/31 13:48
 * @Description:
 */
@Component
public class PairUtils {

    @Autowired
    private StringRedisTemplate redisTemplate;

    private static ConcurrentHashMap pairConcurrentHashMap = new ConcurrentHashMap();
    private static ConcurrentHashMap pairMarketConcurrentHashMap = new ConcurrentHashMap();

    public static void putPairMap(Map pairMap){
        pairConcurrentHashMap.putAll(pairMap);
    }

    public static void putPairMarketMap(Map pairMap){
        pairMarketConcurrentHashMap.putAll(pairMap);
    }

    public static Object getPair(String pairName){
        return pairConcurrentHashMap.get(pairName);
    }

    public static Object getPairMarket(String pairName){
        return pairMarketConcurrentHashMap.get(pairName);
    }

    /**
     *
     * 功能描述:
     *  获取交易币种信息
     * @param: pairName 交易币种名称
     * @return: Pair 交易币种信息实体
     * @auther: dison
     * @date: 2018/8/6 14:30
     */
    public static Pair getPairEntity(String pairName){
        Object obj = getPair(pairName);
        if (obj == null){
            return null;
        }
        return JSON.parseObject(obj.toString(),Pair.class);
    }

    /**
     *
     * 功能描述:
     *      通过交易币种名称获取BTC的数量
     * @param: pairName 交易币种名称
     * @return: 换算比特币的数量
     * @auther: dison
     * @date: 2018/8/6 14:28
     */
    public static BigDecimal getBTCQty(String pairName){

        Object obj = getPairMarket(pairName);
        if (obj == null){
            return null;
        }
        PairMarket pairMarket = JSON.parseObject(obj.toString(),PairMarket.class);
        return Optional.ofNullable(pairMarket.getClose()).orElse(null);
    }

    /**
     *
     * 功能描述:
     *      校验交易币种名称是否正确
     * @param: pairName 交易币种名称
     * @return:
     * @auther: dison
     * @date: 2018/8/6 14:29
     */
    public static boolean verifyPairName(String pairName){
        return ofNullable(getPair(pairName)).isPresent();
    }

    /**
     *
     * 功能描述:
     *      获取币种实时价格（人名币）
     * @param:
     * @return:
     * @auther: dison
     * @date: 2018/8/15 18:56
     */
//    public static BigDecimal getCoinPriceCny(String coinName){
//        CoinmarketcapVo coinmarketcapVo = getConvertCnyInfo(coinName);
//        if (coinmarketcapVo != null && coinmarketcapVo.getPriceCny() != null){
//            return new BigDecimal(coinmarketcapVo.getPriceCny());
//        }
//        return null;
//    }

    /**
     *
     * 功能描述:
     *      获取转换后的币种信息
     * @param:
     * @return:
     * @auther: dison
     * @date: 2018/8/20 18:14
     */
    public CoinmarketcapVo getConvertCnyInfo(String coinName,String unit){
        // 因数据库存储的USDT全称错误，应为Tether，否则导致无法获取实时价格
        if ("USDT".equalsIgnoreCase(coinName)){
            coinName = "Tether";
        }
        String requestUrl = MessageFormat.format(CommonConstant.COINMARKETCAP_COIN_PRICE_CNY_URL, coinName,unit);
        List<CoinmarketcapVo> listCoinmarketcapVo = OkHttpClientUtils.get(requestUrl, null, new TypeReference<List<CoinmarketcapVo>>(){});

        if (!listCoinmarketcapVo.isEmpty()){
            redisTemplate.opsForValue().set(CommonConstant.REDIS_COINMARKET_BTC +":"+coinName + ":" + unit,JSON.toJSONString(listCoinmarketcapVo.get(0)), 24,TimeUnit.HOURS);
        }

        return Optional.ofNullable(listCoinmarketcapVo)
                .map(coins -> coins.get(0))
                .orElse(null);
    }

    public static void main(String[] args) {
    }
}
